/*
 * Copyright 2009 Eat Local Food, LLC
 * Copyright (c) 2007 osCommerce (this file was written after
 * code review of osCommerce).
 *
 * This file is part of gwtCommerce.
 *
 * gwtCommerce is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 2 of the License, or
 * (at your option) any later version.
 *
 * gwtCommerce is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with gwtCommerce.  If not, see <http://www.gnu.org/licenses/>.
 */
package com.elf.gwt.admin.client.components;

import java.util.ArrayList;
import java.util.List;

import com.elf.gwt.admin.client.AdminComponentController;
import com.elf.gwt.common.client.model.TaxClassModel;
import com.google.gwt.json.client.JSONArray;
import com.google.gwt.json.client.JSONObject;
import com.google.gwt.user.client.ui.CheckBox;
import com.google.gwt.user.client.ui.Grid;
import com.google.gwt.user.client.ui.Hidden;
import com.google.gwt.user.client.ui.TextBox;

/**
 *  
 */
public class TaxClassesComponent extends EditTableComponent
{	
	protected List<TaxClassModel> taxClassModels;
	protected TaxClassModel emptyTaxClassModel;

	/**
	 * Constructor
	 */
	public TaxClassesComponent(AdminComponentController controller, String stylePrefix)
	{
		super(controller, stylePrefix);
		emptyTaxClassModel = new TaxClassModel();
		taxClassModels = new ArrayList<TaxClassModel>();
		paginate = true;
		tableType = "tax classes";
	}

	protected void buildMetaModel(JSONObject jsonObject) throws Exception
	{
		super.buildMetaModel(jsonObject);
	}

	protected void buildModel(JSONArray jsonArray) throws Exception
	{
		taxClassModels.clear();
		super.buildModel(jsonArray);
	}

	protected void buildModel(JSONObject jsonObject) throws Exception
	{
		TaxClassModel model = new TaxClassModel(jsonObject);
		taxClassModels.add(model);
	}

	public void buildFromModel() throws Exception
	{
		numberOfTableEntries = taxClassModels.size();
		if (edit)
			numberOfTableColumns = 3;
		else 
			numberOfTableColumns = 2;
		extraRows = 2;
		super.buildFromModel();
	}

	public void displayHeader(Grid grid) {
		grid.setText(0, 0, "Title");
		grid.setText(0, 1, "Description");
		if (edit) grid.setText(0, 2, "Mark for Deletion");
	}

	public void displayRow(Grid grid, int row, int entryIndex) {
		displayRow(grid, row, taxClassModels.get(entryIndex));
	}

	public void displayEmptyRow(Grid grid, int row) {
		displayRow(grid, row, emptyTaxClassModel);
	}

	public void displayRow(Grid grid, int row, TaxClassModel model) {
		if (edit) {
			Hidden idHidden = new Hidden();
			idHidden.setName("taxClassId");
			idHidden.setValue(String.valueOf(model.getId()));
			elements.put("taxClassId-" + row, idHidden);

			TextBox titleTextBox = new TextBox();
			titleTextBox.setWidth("160px");
			titleTextBox.setText(model.getTitle());
			elements.put("taxClassTitle-" + row, titleTextBox);
			grid.setWidget(row, 0, titleTextBox);

			TextBox descTextBox = new TextBox();
			descTextBox.setWidth("400px");
			descTextBox.setText(model.getDescription());
			elements.put("taxClassDescription-" + row, descTextBox);
			grid.setWidget(row, 1, descTextBox);

			if (model.getId() > 0) {
				CheckBox deleteCheckBox = new CheckBox();
				elements.put("taxClassDelete-" + row, deleteCheckBox);
				grid.setWidget(row, 2, deleteCheckBox);
			}
		}
		else {
			grid.setText(row, 0, model.getTitle());
			grid.setText(row, 1, model.getDescription());
		}
	}

	public List<TaxClassModel> getTaxClassModels() {
		return taxClassModels;
	}
}
